/**
 * Package name:indi.yangshenhui.hbase.test
 * File name:RuJiaMain.java
 * Date:2016年4月9日-下午3:01:44
 * feiniu.com Inc.Copyright (c) 2013-2015 All Rights Reserved.
 *
 */
package indi.yangshenhui.hbase.test;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.Deque;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.atomic.AtomicInteger;

import org.apache.commons.lang.StringUtils;
import org.apache.hadoop.hbase.io.hfile.Compression.Algorithm;
import org.apache.hadoop.hbase.regionserver.StoreFile.BloomType;

import indi.yangshenhui.hbase.dao.HBaseHelper;
import indi.yangshenhui.hbase.dao.constant.HBaseConstant;
import indi.yangshenhui.hbase.dao.entity.htable.HBaseTableDescribesEntity;
import indi.yangshenhui.hbase.dao.entity.htable.HBaseTableFamilyDescribesEntity;
import indi.yangshenhui.hbase.dao.exception.HBaseDaoException;
import indi.yangshenhui.hbase.dao.pool.HTablePoolEngine;
import indi.yangshenhui.tool.DateUtil;

/**
 * @ClassName RuJiaMain
 * @Description
 * @date 2016年4月9日 下午3:01:44
 * @author shenhui
 * @version 1.0.0
 *
 */
public class RuJiaMain {
	private static Deque<File> files;
	private static AtomicInteger rnum;
	public static String tName = "rujia_";
	public static int tc;

	static {
		if (files == null) {
			files = new LinkedBlockingDeque<File>();
		}
		if (rnum == null) {
			rnum = new AtomicInteger(0);
		}
	}

	public static void main(String[] args) throws HBaseDaoException, IOException {

		HTablePoolEngine.createHBaseConfiguration(HBaseConstant.HBASE_DEFAULT_INSTANCE, "ubuntu-1,ubuntu-2,ubuntu-3",
				"2181");

		tName = args.length > 2 ? args[2] : tName;

		String path = args.length > 0 ? args[0] : "";
		if (StringUtils.isBlank(path)) {
			path = "C:\\迅雷下载\\2000W";
		}
		File dir = new File(path);

		if (dir.isDirectory()) {
			for (File file : dir.listFiles()) {
				files.add(file);
			}
		}

		tc = args.length > 1 ? Integer.valueOf(args[1]) : 1;

		for (int i = 0; i < tc; i++) {
			RuJiaTask ruJiaTask = new RuJiaTask();
			Thread thread = new Thread(ruJiaTask);
			thread.setDaemon(true);
			thread.start();
		}

		while (true) {
			System.err.println(Thread.currentThread().getName() + " " + DateUtil.format(new Date()) + " " + getRnum());
			try {
				Thread.sleep(1000 * 30);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			if (getRnum() <= 0) {
				for (int i = 0; i < tc; i++) {
					RuJiaTask ruJiaTask = new RuJiaTask();
					Thread thread = new Thread(ruJiaTask);
					thread.setDaemon(true);
					thread.start();
				}
			}
		}

	}

	public static String cTable(String fName) throws HBaseDaoException {
		HBaseTableDescribesEntity hbDescribesEntity = new HBaseTableDescribesEntity();
		hbDescribesEntity.setTableName(tName + fName + DateUtil.getNow(DateUtil.FORMAT_SHORT));
		String[] families = new String[] { "f" };
		Map<String, HBaseTableFamilyDescribesEntity> familyMap = new HashMap<String, HBaseTableFamilyDescribesEntity>();
		// 512MB
		hbDescribesEntity.setMaxFileSize(512 * 1024 * 1024L);
		// 256M
		hbDescribesEntity.setMemstoreFlushSize(256 * 1024 * 1024L);

		// 指定切分的rowKey
		hbDescribesEntity.addSplitRowKey("5");

		hbDescribesEntity.setReadOnly(false);
		hbDescribesEntity.setDeferredLogFlush(false);

		for (String family : families) {
			HBaseTableFamilyDescribesEntity hBaseTableFamilyDescribesEntity = new HBaseTableFamilyDescribesEntity();
			hBaseTableFamilyDescribesEntity.setTimeToLive(Integer.MAX_VALUE);
			hBaseTableFamilyDescribesEntity.setBloomType(BloomType.ROW);
			hBaseTableFamilyDescribesEntity.setCompression(Algorithm.SNAPPY);
			hBaseTableFamilyDescribesEntity.setCompactionCompression(Algorithm.SNAPPY);
			hBaseTableFamilyDescribesEntity.setMaxVersions(1);
			hBaseTableFamilyDescribesEntity.setInMemory(false);
			// 0表示不做修改
			hBaseTableFamilyDescribesEntity.setBlockSize(0);
			hBaseTableFamilyDescribesEntity.setBlockCacheEnabled(true);
			familyMap.put(family, hBaseTableFamilyDescribesEntity);
		}
		hbDescribesEntity.setFamilyMap(familyMap);
		List<HBaseTableDescribesEntity> tList = HBaseHelper.list(HBaseConstant.HBASE_DEFAULT_INSTANCE);
		boolean flag = false;
		for (HBaseTableDescribesEntity t : tList) {
			System.err.println(t.toString());
			if (tName.equals(t.getTableName())) {
				flag = true;
			}
		}

		if (flag == false) {
			flag = HBaseHelper.create(hbDescribesEntity, HBaseConstant.HBASE_DEFAULT_INSTANCE);
			System.err.println("create [" + tName + ":" + flag + "]");
		}

		return hbDescribesEntity.getTableName();
	}

	public static File getFile() {
		return files.poll();
	}

	public static void addFile(File file) {
		files.add(file);
	}

	public static int getRnum() {
		return rnum.get();
	}

	public static void jiaRnum() {
		rnum.incrementAndGet();
	}

	public static void jianRnum() {
		rnum.decrementAndGet();
	}

}
